package com.tms.app.tms.ui;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.amap.api.location.AMapLocation;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.tms.app.tms.R;
import com.tms.app.tms.utils.LocationUtil;
import com.tms.app.tms.utils.MapUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/18 0018.
 */

public class MapAc extends AmapActivity {


    @BindView(R.id.text_nav)
    Button textNav;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_content_1)
    TextView textContent1;
    @BindView(R.id.text_content_2)
    TextView textContent2;
    private String title="";
    private String adress="";
    private String content="";
    private String lat;
    private String lon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        ButterKnife.bind(this);
        initView();
        lat = getIntent().getStringExtra("lat");
        lon = getIntent().getStringExtra("lon");
        if (StringUtil.isEmpty(lat)||StringUtil.isEmpty(lon)){
            showMsg("坐标错误！");
            finish();
        }
        LatLng latLng = new LatLng(Double.parseDouble(lat),Double.parseDouble(lon));
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        //设置中心点
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
        MarkerOptions markerOption = new MarkerOptions();
        markerOption.position(latLng);
        markerOption.draggable(false);//设置Marker可拖动
        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.marker)));
        // 将Marker设置为贴地显示，可以双指下拉地图查看效果
        markerOption.setFlat(true);//设置marker平贴地图效果
        aMap.addMarker(markerOption.snippet(title+""));
        //设置地图的放缩级别
        aMap.moveCamera(CameraUpdateFactory.zoomTo(12));
        aMap.showIndoorMap(true);  //true：显示室内地图；false：不显示；
        aMap.setLocationSource(this); // 设置定位监听
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        myLocationStyle = LocationUtil.getMyLocationStyle();
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.getUiSettings().setZoomControlsEnabled(false);
        aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示，非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                //从location对象中获取经纬度信息，地址描述信息，建议拿到位置之后调用逆地理编码接口获取
            }
        });
    }


    @OnClick({R.id.text_nav})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text_nav:
                if (!MapUtil.isAvilible(this, "com.autonavi.minimap")) {
                    showMsg("您的手机未安装高德地图，请安装地图");
                    return;
                }
                Intent intent = new Intent("android.intent.action.VIEW",
                        Uri.parse("androidamap://route?sourceApplication=softname&dlat=" +
                                lat +
                                "&dlon=" +
                                lon +
                                "&dname=" +
                                title +
                                "&dev=0&m=0&t=0&showType=1"));
                intent.setPackage("com.autonavi.minimap");
                startActivity(intent);
//                Intent intent = new Intent("android.intent.action.VIEW",
//                        Uri.parse("androidamap://route?sourceApplication=softname&slat=30.6739968716&slon=103.9602246880&sname=起始点&dlat=31.192612&dlon=116.407525&dname=湘江大厦&dev=0&m=0&t=0&showType=1"));
//                intent.setPackage("com.autonavi.minimap");
//                startActivity(intent);
                break;
        }
    }


    private void initView() {
        title = getIntent().getStringExtra("title");
        adress = getIntent().getStringExtra("adress");// intent.putExtra("content", "电风扇地方");
        content = getIntent().getStringExtra("content");// intent.putExtra("lat", LBS[1]);
        textTitle.setText(title+"");
        textContent1.setText("联系人：" +content);
        textContent2.setText("地址："+adress);

    }

    //--定位监听---------
    //定位回调  在回调方法中调用“mListener.onLocationChanged(amapLocation);”可以在地图上显示系统小蓝点。
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (mListener != null && aMapLocation != null) {
            if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点

            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
                Log.e("定位AmapErr", errText);
            }
        }
    }


}